import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { BusinessException } from 'src/common/exceptions/business.exception'
import { Asset } from 'src/entity/Asset'
import { Easter } from 'src/entity/Easter'
import { Task } from 'src/entity/Task'
import { Connection, IsNull, Repository } from 'typeorm'
import { v4 as uuidV4 } from 'uuid'

@Injectable()
export class EasterService {
  constructor(
    @InjectRepository(Easter)
    private easterRepository: Repository<Easter>,
    private connection: Connection,
  ) { }

  async add(data) {
    const easterCode = 'E' + uuidV4()
    data.easterCode = easterCode
    data.easterType = '0'

    await this.connection.transaction(async (manager) => {
      const assetSqlData = await manager.findOne(Asset, {
        where: {
          isDelete: true,
          assetCode: data.assetCode,
        },
      })
      if (!assetSqlData)
        throw new BusinessException('该资产编号不存在或者未报废')
      data.assetName = assetSqlData.assetName
      data.easterTypeId = assetSqlData.typeId
      const sqlData = await manager.save(Easter, data)
      const taskData = new Task()
      taskData.receiveBranch = sqlData.receiveBranch
      taskData.receiveBranchId = sqlData.receiveBranchId
      taskData.receivePerson = sqlData.receivePerson
      taskData.receivePersonId = sqlData.receivePersonId
      taskData.receiveCode = sqlData.easterCode
      taskData.receiveDate = sqlData.receiveDate
      taskData.taskName = sqlData.receivePerson + '的资产复活申请'
      taskData.taskStatus = sqlData.easterType
      taskData.taskType = '7'
      await manager.save(Task, taskData)
    })

    return data
  }

  async del(id: number) {
    let data
    await this.connection.transaction(async (manager) => {
      const sqlData = await this.easterRepository.findOne({
        where: {
          id,
        },
      })
      await manager.update(
        Task,
        {
          receiveCode: sqlData.easterCode,
        },
        {
          isDelete: true,
        },
      )
      data = await manager.update(
        Easter,
        {
          id: id,
        },
        {
          isDelete: true,
        },
      )
    })
    return data
  }

  async update(data) {
    data.easterType = '0'
    data.reason = null
    let sqlData
    await this.connection.transaction(async (manager) => {
      await manager.update(
        Task,
        {
          receiveCode: data.easterCode,
        },
        {
          taskStatus: data.easterType,
          reason: null,
          taskDate: null,
          taskPersonId: null,
          taskPerson: null,
        },
      )
      sqlData = await this.easterRepository.update(
        {
          id: data.id,
        },
        data,
      )
    })
    return sqlData
  }

  // 分页查询
  async getPageList({
    pageRequest: { pageIndex, pageSize },
    easterCode,
    assetName,
    easterTypeId,
    receivePerson,
    easterType,
    orgId,
  }) {
    const sqlAllData = await this.easterRepository.find({
      where: {
        isDelete: IsNull(),
        receiveBranchId: orgId,
        easterCode: easterCode,
        assetName: assetName,
        easterTypeId: easterTypeId ? parseInt(easterTypeId) : undefined,
        receivePerson: receivePerson,
        easterType: easterType,
      },
    })
    const sqlData = await this.easterRepository.find({
      where: {
        isDelete: IsNull(),
        receiveBranchId: orgId,
        easterCode: easterCode,
        assetName: assetName,
        easterTypeId: easterTypeId ? parseInt(easterTypeId) : undefined,
        receivePerson: receivePerson,
        easterType: easterType,
      },
      skip: (pageIndex - 1) * pageSize,
      take: pageSize,
      withDeleted: false,
    })

    return {
      content: sqlData.map((item) => {
        return {
          ...item,
          easterTypeId: item.easterTypeId.toString(),
        }
      }),
      total: sqlAllData.length,
    }
  }

  // 获取复活详情 easterCode -> *
  async getDetailByCode(code) {
    const sqlData = await this.easterRepository.findOne({
      where: {
        easterCode: code,
        isDelete: IsNull(),
      },
    })
    return {
      ...sqlData,
      easterTypeId: sqlData.easterTypeId.toString(),
    }
  }

  // 获取复活详情
  async getEasterDetail(id) {
    return await this.easterRepository.findOne({
      where: {
        isDelete: IsNull(),
        id,
      },
    })
  }
}
